import torch
import torch.utils.data
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import numpy as np
from torchvision import transforms

import glob
from PIL import Image


all_img_path = glob.glob(r'D:\learn\深度学习\四种天气图片数据集\四种天气图片数据集\dataset2\*.jpg')

species = ['cloudy', 'rain', 'shine', 'sunrise']
species_to_idx = dict((c, i) for i, c in enumerate(species))
print(species_to_idx)
idx_to_species = dict((v, k) for k, v in species_to_idx.items())
print(idx_to_species)

all_img_size = len(all_img_path)
all_labels = []
# 借助ndarray的索引取值的方法，打乱数据
random_index = np.random.permutation(all_img_size)
all_img_path = np.array(all_img_path)[random_index]
for img in all_img_path:
    for i, c in enumerate(species):
        if c in img:
            all_labels.append(i)

print(all_labels)

split = int(all_img_size * 0.8)

train_imgs = all_img_path[:split]
train_labels = all_labels[:split]

test_imgs = all_img_path[split:]
test_labels = all_labels[split:]

transform = transforms.Compose([
    transforms.Resize((96, 96)),
    transforms.ToTensor()
])


class MyDataset(torch.utils.data.Dataset):
    def __init__(self, img_paths, labels, transform):
        super().__init__()
        self.imgs = img_paths
        self.labels = labels
        self.transforms = transform

    def __getitem__(self, i):
        # 通过PIL的Image读取图片
        img = Image.open(self.imgs[i])
        if np.array(img).shape[-1] == 3:
            data = self.transforms(img)
            return data,  self.labels[i]
        else:
            return self.__getitem__(i + 1)

    def __len__(self):
        return len(self.imgs)

    @staticmethod
    def collate_fn(batch):
        # batch是个列表
        # 列表的每个元素是一个元组（x, y）
        batch = [temp for temp in batch if temp is not None]
        from torch.utils.data.dataloader import  default_collate
        return default_collate(batch)

train_ds = MyDataset(train_imgs, train_labels, transform)
test_ds = MyDataset(test_imgs, test_labels, transform)

batch_size = 16
train_dl = torch.utils.data.DataLoader(train_ds, batch_size=batch_size, shuffle=True, collate_fn=MyDataset.collate_fn)
test_dl = torch.utils.data.DataLoader(test_ds, batch_size=batch_size*2, collate_fn=MyDataset.collate_fn)
imgs, labels = next((iter(train_dl)))

print(imgs.shape)


class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 16, 3) #16*94*94
        self.bn1 = nn.BatchNorm2d(16)
        self.pool = nn.MaxPool2d(2, 2) #16*47*47
        self.conv2 = nn.Conv2d(16, 32, 3) #32*45*45 -> pooling -> 32*32*32
        self.bn2 = nn.BatchNorm2d(32)
        self.conv3 = nn.Conv2d(32, 64, 3)  #64*20*20 -> pooling -> 64*10*10
        self.bn3 = nn.BatchNorm2d(64)
        self.dropout = nn.Dropout()

        #batch,
        self.fc1 = nn.Linear(64*10*10, 1024)
        self.bn_fc1 = nn.BatchNorm1d(1024)
        self.fc2 = nn.Linear(1024, 256)
        self.bn_fc2 = nn.BatchNorm1d(256)
        self.fc3 = nn.Linear(256, 4)

    def forward(self, x):
        x = self.pool(self.bn1(self.conv1(x)))
        x = self.pool(self.conv2(x))
        x = self.pool(self.conv3(x))

        x = nn.Flatten()(x)
        x = F.relu(self.fc1(x))
        x = self.dropout(x)
        x = F.relu(self.fc2(x))
        x = self.dropout(x)
        x = self.fc3(x)
        return x

model = Net()

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
model.to(device)
optimizer = optim.Adam(model.parameters(), lr=0.001)
loss_fn = nn.CrossEntropyLoss()


def fit(epoch, model, train_loader, test_loader):
    correct = 0
    total = 0
    running_loss = 0
    model.train()
    for x, y in train_loader:
        # 把数据放到GPU上去
        x, y = x.to(device), y.to(device)
        y_pred = model(x)
        loss = loss_fn(y_pred, y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        with torch.no_grad():
            y_pred = torch.argmax(y_pred, dim=1)
            correct += (y_pred == y).sum().item()
            total += y.size(0)
            running_loss += loss.item()

    epoch_loss = running_loss / len(train_loader.dataset)
    epoch_acc = correct / total

    # 测试过程
    test_correct = 0
    test_total = 0
    test_running_loss = 0
    model.eval()
    with torch.no_grad():
        for x, y in test_loader:
            x, y = x.to(device), y.to(device)
            y_pred = model(x)
            loss = loss_fn(y_pred, y)
            y_pred = torch.argmax(y_pred, dim=1)
            test_correct += (y_pred == y).sum().item()
            test_total += y.size(0)
            test_running_loss += loss.item()

    test_epoch_loss = test_running_loss / len(test_loader.dataset)
    test_epoch_acc = test_correct / test_total

    print('epoch:', epoch,
          'loss:', round(epoch_loss, 3),
          'accuracy:', round(epoch_acc, 3),
          'test_loss:', round(test_epoch_loss, 3),
          'test_accuracy:', round(test_epoch_acc, 3)
          )

    return epoch_loss, epoch_acc, test_epoch_loss, test_epoch_acc

epochs = 20
train_loss = []
train_acc = []
test_loss = []
test_acc = []
for epoch in range(epochs):
    epoch_loss, epoch_acc, test_epoch_loss, test_epoch_acc = fit(epoch, model, train_dl, test_dl)
    train_loss.append(epoch_loss)
    train_acc.append(epoch_acc)
    test_loss.append(test_epoch_loss)
    test_acc.append(test_epoch_acc)



















